import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);
import { SignIn } from "@/api/user";
import userPlugins from './userPlugins';
export default new Vuex.Store({
	state: {
		user: null,
		userId: null,
		Ticket: null
	},
	mutations: {
		"setuser": (state, data) => {
			state.user = data;
			state.userId = data?.F_UserId || null;
		},
		"setTicket": (state, data) => {
			state.Ticket = data;
		}
	},
	actions: {
		login({ commit }, userInfo) {
			return new Promise((resolve, reject) => {
				SignIn(userInfo).then(({ data }) => {
					commit('setuser', data.Data);
					commit('setTicket', data.Ticket);
					resolve();
				}).catch(error => {
					reject(error);
				})
			})
		},
		Logout({ commit }) {
			return new Promise((resolve) => {
				commit('setuser', null);
				resolve()
			})
		}
	},
	plugins: [userPlugins]
});
